<template>
  <div class="find">
    <van-nav-bar title="搜索页" />
    <van-search
      shape="round"
      v-model="value"
      placeholder="请输入搜索关键词"
      @search="onSearch"
      @input="onInput"
    />
    <!-- 搜索下容器 -->
    <div class="search_wrap">
      <!-- 标题 -->
      <p class="hot_title">热门搜索</p>
      <!-- 热搜关键词容器 -->
      <div class="hot_name_wrap" v-if="result.length === 0">
        <!-- 每个搜索关键词 -->
        <span 
        class="hot_item"
        v-for="(item,index) in hotList"
        :key="index"
        @click="value=item.first;onSearch()"
        >{{ item.first }}</span>
      </div>
      <!-- 搜索结果 -->
      <div class="search_wrap" v-else>
        <!-- 标题 -->
        <p class="hot_title">最佳匹配</p>
        <van-cell
          v-for="item in result"
          :key="item.id"
          :title="item.name"
          size="large"
          :label="item.artists[0].name"
          @click="$router.push({ name: 'play', query: { id: item.id } })"
        >
          <template #right-icon>
            <van-icon name="play-circle-o" class="play-icon" />
          </template>
        </van-cell>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'find',
  data () {
    return {
      // 搜索关键词
      value: '',
      //搜索结果
      result: [],
      //热搜列表
      hotList:[]
    }
  },
  methods: {
    onSearch () {
      //enter键触发
      this.$axios({
        url: '/search',
        method: 'get',
        params: { keywords: this.value }
      }).then(res => {
        //成功回调
        console.log(res)
        this.result = res.data.result.songs
      })
    },
    onInput () {
      //内容变化触发
      //判断是否为空，为空则清空搜索列表
      if (this.value.length === 0) {
        this.result = []
      }
    }
  },
  //生命周期钩子
  created() {
    this.$axios({
      url:'/search/hot',
      method:'get',
    }).then(res=>{
      //成功回调
      console.log(res)
      this.hotList = res.data.result.hots
    });
  },
}
</script>

<style scoped lang="less">
/* 搜索容器的样式 */
.search_wrap {
  padding: 0.266667rem;
}

/*热门搜索文字标题样式 */
.hot_title {
  font-size: 0.32rem;
  color: #666;
}

/* 热搜词_容器 */
.hot_name_wrap {
  margin: 0.266667rem 0;
}

/* 热搜词_样式 */
.hot_item {
  display: inline-block;
  height: 32px;
  margin-right: 8px;
  margin-bottom: 8px;
  padding: 0 14px;
  font-size: 14px;
  line-height: 32px;
  color: #333;
  border-color: #d3d4da;
  border-radius: 0.853333rem;
  border: 1px solid #d3d4da;
}

/* 给单元格设置底部边框 */
.van-cell {
  border-bottom: 1px solid lightgray;
}
.find .play-icon {
  font-size: 30px;
  color: gray;
}
.find {
  .van-cell__title {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
</style>
